package controllers

import (
	"fly/helper"
	"fly/models"
	"fly/req"
	"fly/resp"
	"fly/service"
	"github.com/duke-git/lancet/v2/convertor"
	"net/url"
)

// AircraftComponentController 飞机组件
type AircraftComponentController struct {
	BaseController
	AircraftComponentService service.AircraftComponentService
	AircraftService          service.AircraftService
	SysUserService           service.SysUserService
	SysDeptService           service.SysDeptService
	SysDictService           service.SysDictService
}

// Components ...
// @Title 组件列表
// @Description 查询飞机下的组件列表
// @Param token header  string	false "授权Token"
// @Success 200 {object} []resp.AircraftComponentResp
// @router /components/:aircraftId [get]
func (c *AircraftComponentController) Components() {
	aircraftId, err := convertor.ToInt(c.Ctx.Input.Param(":aircraftId"))
	c.CheckReqErrorAbort(err)
	con, err := c.AircraftService.GetSimpleAircraftById(aircraftId)
	c.CheckAssertAbort(con, "未找到飞机")
	list, err := c.AircraftComponentService.GetAircraftComponents(aircraftId)
	var componentList []resp.AircraftComponentResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&componentList, &list))
	for i, airport := range list {
		componentList[i].DeptName, _ = c.SysDeptService.GetSimpleDeptNameById(airport.DeptId)
	}
	c.Success(componentList)
}

// Save ...
// @Title 添加或者编辑飞机组件
// @Description 添加或者编辑飞机组件
// @Param token header  string	false "授权Token"
// @Param body body req.AircraftComponentSaveReq true "添加或者编辑飞机组件"
// @Success 200 {object} resp.AircraftComponentResp
// @router /save [post]
func (c *AircraftComponentController) Save() {
	con := req.AircraftComponentSaveReq{}
	c.CheckReqErrorAbort(c.BindJSON(&con))
	c.ValidReqParams(&con)
	mComponent := models.AircraftComponent{}
	c.CheckRespErrorAbort(helper.CopyDeep(&mComponent, con))
	mComponent.FactoryTime = helper.Int2TimePoint(con.FactoryTime)
	mComponent.StartUseTime = helper.Int2TimePoint(con.StartUseTime)
	mComponent.AcceptanceTime = helper.Int2TimePoint(con.AcceptanceTime)
	id, err := c.AircraftComponentService.AddOrUpdateComponent(&mComponent, c.GetReqUserId())
	c.CheckRespErrorAbort(err)
	r := resp.AircraftComponentResp{}
	component, err := models.GetAircraftComponentById(id)
	_ = resp.Copy2TimeResp(&r, component)
	c.Success(r)
}

// Del ...
// @Title 删除组件
// @Description 删除组件
// @Param token header  string	false "授权Token"
// @Param path id int true "组件id"
// @Success 200 {int} "id"
// @router /:id [delete]
func (c *AircraftComponentController) Del() {
	id, err := convertor.ToInt(c.Ctx.Input.Param(":id"))
	c.CheckReqErrorAbort(err)
	c.CheckReqErrorAbort(helper.RequireTrue(c.SysUserService.IsSuperAdminUser(c.GetReqUserId()), "超级管理员才能操作"))
	c.CheckRespErrorAbort(models.DeleteAircraftComponent(id))
	c.Success(id)
}

// Index ...
// @Title 组件列表
// @Description 组件列表
// @Param token header  string	false "授权Token"
// @Param keywords query  string false "搜索字段"
// @Param pageNum query  int false "当前页"
// @Param pageSize query  int false "每页数量"
// @Param total query  int false "数据总数"
// @Param type query  string false "配件类型"
// @Param aircraftCode query  int false "飞机code"
// @Param code query  int false "组件code"
// @Success 200 {object} []service.Pagination
// @router / [get]
func (c *AircraftComponentController) Index() {
	params, _ := url.ParseQuery(c.Ctx.Request.URL.RawQuery)
	data, pagination := c.AircraftComponentService.GetPaginateData(params)
	var componentList []resp.AircraftComponentResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&componentList, &data))
	for i, component := range data {
		componentList[i].CreatorName, _ = c.SysUserService.GetUserRealNameById(component.Creator)
		componentList[i].UpdaterName, _ = c.SysUserService.GetUserRealNameById(component.Updater)
		componentList[i].DeptName, _ = c.SysDeptService.GetSimpleDeptNameById(component.DeptId)
		componentList[i].StateName, _ = c.SysDictService.GetDictValueById(component.StateDictId)
	}
	pagination.List = componentList
	c.Success(pagination)
}

// List ...
// @Title 部门下所有可用飞机组件
// @Description 所有飞机组件-用于首页显示
// @Param token header  string	false "授权Token"
// @Param type query  int false "飞机类型"
// @Success 200 {object} []resp.AircraftComponentResp
// @router /list [get]
func (c *AircraftComponentController) List() {
	subIds, err := c.SysDeptService.GetSubDeptIdListByUserId(c.GetReqUserId())
	c.CheckRespErrorAbort(err)
	list, err := c.AircraftComponentService.GetDeptComponents(subIds)
	c.CheckRespErrorAbort(err)
	var componentList []resp.AircraftComponentResp
	c.CheckRespErrorAbort(resp.Copy2TimeResp(&componentList, &list))
	for i, component := range list {
		componentList[i].CreatorName, _ = c.SysUserService.GetUserRealNameById(component.Creator)
		componentList[i].UpdaterName, _ = c.SysUserService.GetUserRealNameById(component.Updater)
		componentList[i].DeptName, _ = c.SysDeptService.GetSimpleDeptNameById(component.DeptId)
		componentList[i].StateName, _ = c.SysDictService.GetDictValueById(component.DeptId)
	}
	c.Success(componentList)
}
